package cn.Alibaba.org.controller;


import cn.Alibaba.basic.util.JsonResult;
import cn.Alibaba.basic.util.PageList;
import cn.Alibaba.org.domain.Employee;
import cn.Alibaba.org.query.EmployeeQuery;
import cn.Alibaba.org.service.IEmployeeService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RequestMapping("/employee")
@Api(value = "员工的API",description="员工相关的CRUD功能")
@RestController
public class EmployeeController {

    @Autowired
    private IEmployeeService service;


    /**
     * 查询所有
     * @return
     */
    @GetMapping
    @ApiOperation(value = "查询展示所有数据" )
    public List<Employee> findAll(){
        return service.findAll();
    }

    /**
     * 查询单个
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    @ApiOperation(value = "通过ID查询数据" )
    public Employee findOne(@PathVariable("id") Long id){
        return service.findOne(id);

    }

    /**
     * 删除操作
     * @param id
     * @return
     */
    @DeleteMapping("/{id}")
    @ApiOperation(value = "通过ID删除数据" )
    public JsonResult delete(@PathVariable("id") Long id){

        try {
            service.del(id);
            return JsonResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return JsonResult.me().setMsg("操作失败");
        }

    }

    @PatchMapping
    @ApiOperation(value = "通过ID删除数据" )
    public JsonResult PatchDel(@RequestBody List<Long> ids){

        try {
            service.PatchDel( ids);
            return JsonResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return JsonResult.me().setMsg("操作失败");
        }

    }

    /**
     * 添加修改
     * @param employee
     * @return
     */
    @PutMapping
    @ApiOperation(value = "通过ID添加或者修改数据" )
    public JsonResult addOrUpdate(@RequestBody  Employee employee ){
        try {
            if (employee.getId()==null){
                service.save(employee);
            }else {
                service.update(employee);
            }

            return JsonResult.me();
        } catch (Exception e) {
            e.printStackTrace();

            return JsonResult.me().setMsg("操作失败");
        }
    }

    /**
     * 分页
     * @param query
     * @return
     */
    @PostMapping
    @ApiOperation(value = "分页查询数据" )
    public PageList<Employee> querPage(@RequestBody EmployeeQuery query){

        return service.queryPage(query);

    }

    /**
     * 查询所有
     * @return
     */
    @GetMapping("/list/{shopId}")
    public List<Employee> list(@PathVariable("shopId") Long shopId){
        return service.list(shopId);
    }




}
